<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_计算器练习</title>
</head>
<body>

<input type="text" id="i1" placeholder="请输入数字1">
<input type="text" id="i2" placeholder="请输入数字2">
<br>
<button onclick="compute('+')">加</button>
<button onclick="compute('-')">减</button>
<button onclick="compute('*')">乘</button>
<button onclick="compute('/')">除</button>
<h3>运算结果：<span id="result"></span></h3>

<script>
    function compute(op){
        let input1=document.querySelector('#i1');
        let input2=document.getElementById('i2');
        let result=document.querySelector('#result');
        let num1=parseFloat(input1.value);
        let num2=parseFloat(input2.value);
        //只有当计算除法时，判断分母不为0,提示信息

        /*if (op=='/'){
            if(num2==0){
                alert('分母不能为0');
                return;
            }
        }*/

        console.log(num1,num2);
        console.log(op);
        switch (op){
            case '+':
                result.innerHTML=num1+num2;
                break;
            case '-':
                result.innerHTML=num1-num2;
                break;
            case '*':
                result.innerHTML=num1*num2;
                break;
            case '/':
                if(num2==0){
                    alert('分母不能为0');
                }else {
                    result.innerHTML=num1/num2;
                    break;
                }


        }
    }
</script>
</body>
</html>